package com.asia5b.a5mall.shop.adapter

import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5bmall.same.databinding.GoodsitemShopcarBinding
import com.asia5b.a5mall.middleware.model.bean.CartMultiItemEntity
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.model.shop.AbsShopModel
import com.asia5b.a5mall.middleware.utils.CurrencyPriceBuilder3
import com.asia5b.a5mall.middleware.widget.PlusView
import com.asia5b.a5mall.shop.R
import com.asia5b.a5mall.shop.databinding.ItemShopcarShopBinding
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * Created by zhangyuncai on 2020/1/4.
 */
class ShopCartAdapter(
        val list: ArrayList<CartMultiItemEntity>
) : BaseMultiItemQuickAdapter<CartMultiItemEntity, BaseDataBindingHolder<*>>(list) {
    var isShowDelete = false

    init {
        addItemType(CartMultiItemEntity.CartShopItem, R.layout.item_shopcar_shop)
        addItemType(CartMultiItemEntity.Holder, R.layout.item_shopcar_holder)
        addItemType(CartMultiItemEntity.CartGoodsItem, R.layout.goodsitem_shopcar)

        addChildClickViewIds(R.id.cb_shopcar_shop_check)
        //店铺选中
        addChildClickViewIds(R.id.cb_shopcar_shop_check_delete)
        //优惠券
        addChildClickViewIds(R.id.tv_shopcar_coupon)

        //增加
        addChildClickViewIds(R.id.tv_plusview_add)
        //减
        addChildClickViewIds(R.id.tv_plusview_sub)
        //复选框
        addChildClickViewIds(R.id.cb_shopcar_goods_check)
        //复选框删除
        addChildClickViewIds(R.id.cb_shopcar_goods_check_delete)
        //关注
        addChildClickViewIds(R.id.tv_shopcar_collect)
        //删除
        addChildClickViewIds(R.id.tv_shopcar_delete)
    }


    override fun convert(helper: BaseDataBindingHolder<*>, item: CartMultiItemEntity) {
        when (item.itemType) {
            CartMultiItemEntity.CartShopItem -> {
                setCartShopItemView(helper, item as AbsShopModel,helper.dataBinding as  ItemShopcarShopBinding)
            }
            CartMultiItemEntity.Holder -> {
            }
            CartMultiItemEntity.CartGoodsItem -> {
                setCartGoodsItemView(helper, item as AbsGoodsModel,helper.dataBinding as GoodsitemShopcarBinding)
            }
        }
    }

    /**
     * 购物车店铺Item适配器
     */

    fun setCartShopItemView(helper: BaseViewHolder, shopItemData: AbsShopModel, binding: ItemShopcarShopBinding) {

        binding.data = shopItemData
        binding.isShowDelete = isShowDelete


    }

    /**
     * 购物车商品Item适配器
     */
    fun setCartGoodsItemView(helper: BaseViewHolder, cartGoodsItemViewModel: AbsGoodsModel, binding: GoodsitemShopcarBinding) {
        binding.data = cartGoodsItemViewModel
        binding.isShowDelete = isShowDelete

        //加减数量
        val plusView = helper.getView<PlusView>(R.id.plus_shopcar)
        plusView.setQty(cartGoodsItemViewModel.absGoodsSetNum)
        plusView.setMinQty(1)
        plusView.setMaxQty(1000)

        //价格
        val tv_shopcar_price = helper.getView<TextView>(R.id.tv_shopcar_price)
        CurrencyPriceBuilder3().run {
            textView = tv_shopcar_price
            price = cartGoodsItemViewModel.absGoodsPrice
            priceList = cartGoodsItemViewModel.absGoodsPriceList
            //价格显示红色
            markAttrBean= CurrencyPriceBuilder3.AttrBean(false,R.color.color_red,14)
            pointAfterAttrBean=CurrencyPriceBuilder3.AttrBean(true,R.color.color_red,18)
            pointBeforeAttrBean=CurrencyPriceBuilder3.AttrBean(false,R.color.color_red,14)
            build14_18()
        }

        //重量
        val tv_shopcar_weight = helper.getView<TextView>(R.id.tv_shopcar_weight)
        tv_shopcar_weight.text = String.format(ResString.get(R.string.重量_数), cartGoodsItemViewModel.absGoodsWeight)



    }
}